package com.xiguthings.waka.web.controller.wechat.wx;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xiguthings.waka.web.controller.wechat.wx.beans.Msg;
import com.xiguthings.waka.web.controller.wechat.wx.beans.MsgType;
import com.xiguthings.waka.web.controller.wechat.wx.beans.msgbody.TextBody;

import java.io.*;


public class WxUtil {

    public static void main(String[] args) throws Exception {
//		Logger log = Logger.getLogger(WxUtil.class);
//		WxConnExec.delMenu();
        createMenu();
//		JSONObject material = WxConnExec.getMaterial("news");//获取素材
//		System.out.println(material);
//		String psw = MD5Util.getMD5Code("11111111");
//		System.out.println(psw);
//		String encode = URLEncoder.encode("http://westv.nat123.cc/wechat_proxy_web/s/owner/index.cgi", "UTF-8");
//		System.out.println(encode);
    }

    /**
     * 更新菜单的方法
     *
     * @throws FileNotFoundException
     * @throws IOException
     */
    private static void createMenu() throws FileNotFoundException, IOException {
        String jsonMenu = "";
        InputStream in = new FileInputStream(
                new File(WxUtil.class.getResource("/").getPath() + "config/wechat_button_test.txt"));
//		InputStream in = new FileInputStream(
//				new File(WxUtil.class.getResource("/").getPath() + "config/wechat_button.txt"));
        // InputStream in =
        // WxUtil.class.getResourceAsStream("config/wechat_button.txt");
        byte[] b = new byte[1024];
        int len = 0;
        while ((len = in.read(b)) != -1) {
            jsonMenu += new String(b, 0, len);
        }
        jsonMenu = jsonMenu.replace("\r", "");
        jsonMenu = jsonMenu.replace("\n", "");
        jsonMenu = jsonMenu.replace("\t", "");
        jsonMenu = jsonMenu.replace(" ", "");
        jsonMenu = jsonMenu.replace("#APPID#", WxConnExec.getWechatAppid());
        System.out.println(jsonMenu);

        WxConnExec.createMenu(jsonMenu);
    }

    /**
     * 发送微信文本消息
     *
     * @param openid
     * @param contentStr
     */
    public static JSONObject sendTextMsg(String openid, String contentStr) {
        Msg msg = new Msg();
        msg.setTouser(openid);
        msg.setMsgtype(MsgType.TEXT);
        TextBody textBody = new TextBody();
        msg.setText(textBody);
        textBody.setContent(contentStr);
        String jsonString = JSON.toJSONString(msg);
        JSONObject sendMsg = WxConnExec.sendMsg(jsonString);
        return sendMsg;

    }
}
